package com.kuanter.kuanterauto.view;

import com.kuanter.kuanterauto.activity.SitesActivity;
import com.kuanter.kuanterauto.adapter.SitesAdapter;

import android.R.integer;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

public class PollListView extends ListView implements OnScrollListener {
	
	private int visibleLastIndex = 0;   //最后的可视项索引  
    private int visibleItemCount;       // 当前窗口可见项总数  

	public PollListView(Context context) {
		super(context);
	}

	public PollListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public PollListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOnScrollListener(this);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
	/*	Log.i("aaa", firstVisibleItem + "--" + visibleItemCount + "--" + totalItemCount);
		if(firstVisibleItem + visibleItemCount == totalItemCount) {
			Log.i("aaa", firstVisibleItem + "--" + visibleItemCount + "--" + totalItemCount + "底部");
			//滑倒底部了
			SitesAdapter adapter = (SitesAdapter) getAdapter();
			if(adapter != null) {
				if(adapter.getDatasSize() > adapter.getCount()) {
					int page = adapter.getCurrentPage();
					page++;
					adapter.setCurrentPage(page);
					adapter.notifyDataSetChanged();
				} else if(adapter.getDatasSize() == adapter.getCount()) {
					if(handler != null) {
						handler.sendEmptyMessage(SitesActivity.POLLLIST);
					}
				}
				
			}
			
		}*/
		this.visibleItemCount = visibleItemCount;
		visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		int itemsLastIndex = getAdapter().getCount() - 1;
//		int lastIndex = itemsLastIndex + 1;  //底部的loadmore
		if(scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == itemsLastIndex) {
//			SitesAdapter adapter = (SitesAdapter) getAdapter();
			HeaderViewListAdapter headerAdapter = (HeaderViewListAdapter) getAdapter();
			SitesAdapter adapter = (SitesAdapter) headerAdapter.getWrappedAdapter();
			if(adapter != null) {
				int page = adapter.getCurrentPage();
				if(adapter.getDatasSize() > adapter.getCount()) {
					page++;
					adapter.setCurrentPage(page);
					adapter.notifyDataSetChanged();
				} else if(adapter.getDatasSize() == adapter.getCount()) {  //&& adapter.getDatasSize() != 0 
					if(handler != null) {
						page++; 
						adapter.setCurrentPage(page);
						handler.sendEmptyMessage(SitesActivity.POLLLIST);
					}
					
				}
				
			}
		}
	}

	Handler handler;
	public void setHandler(Handler handler) {
		this.handler = handler;
	}
	
	
}
